﻿Imports System.Xml
Public Class frmStory
    Const Max = 20
    Dim j As Integer = 0
    Dim textbox(Max) As TextBox
    Dim arrNode As New ArrayList
    Dim idXmlSelect As Integer
    Public filename As String
    Public mode As String = "New"
    Private Function GetRecord(ByVal file_name As String) As Integer
        Try
            Dim xmldoc As New XmlDocument
            Dim Doc As New XmlDocument
            xmldoc.Load(file_name)
            Dim node As XmlNode = xmldoc.SelectSingleNode("/RootSubject")
            Dim a As String = node.ChildNodes(node.ChildNodes.Count() - 1).Attributes("id").Value
            Return Int64.Parse(a)
        Catch ex As Exception

        End Try

    End Function
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.Filter = "Text Files|*.th"

        If mode = "Old" Then
            Remove_Node_Xml(filename)
            Save_Gview_To_Xml(filename)
        ElseIf mode = "New" Then
            SaveFileDialog.ShowDialog()
            filename = SaveFileDialog.FileName
            If filename = String.Empty Then
                Exit Sub
            End If
            CreatXml(filename)
            Save_Gview_To_Xml(filename)
            mode = "Old"
        End If
        ClearText()
    End Sub
    Private Sub Save_Gview_To_Xml(ByVal file_name As String)
        For n = 0 To gVStory.Rows.Count - 1
            Dim Doc As New XmlDocument
            Doc.Load(file_name)
            ' Tạo một phần tử mới.
            Dim ItemNode As XmlNode
            ItemNode = Doc.CreateElement("Story")
            ' Thêm đặc tính.
            Dim Attribute As XmlAttribute
            Attribute = Doc.CreateAttribute("id")
            Attribute.Value = GetRecord(file_name) + 1
            ItemNode.Attributes.Append(Attribute)
            ' Tạo và thêm các phần tử con cho nút này.
            Dim Node(Max) As XmlNode
            Dim i As Integer
            For i = 0 To j - 1
                Node(i) = Doc.CreateElement(arrNode(i).ToString)
                ItemNode.AppendChild(Node(i))
                Node(i).AppendChild(Doc.CreateTextNode(gVStory.Rows(n).Cells(i).Value))
                'MsgBox(textbox(i).Text)
            Next
            ' Thêm phần tử mới. Trong trường hợp này, chúng ta
            ' cho nó làm con ở cuối danh sách item.
            Doc.DocumentElement.AppendChild(ItemNode)
            ' Lưu tài liệu.
            Doc.Save(file_name)
        Next
        MsgBox("Save successful", MsgBoxStyle.Information)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'filename = "C:\12.xml"
        'mode = "Old"
        If mode = "Old" Then
            Load_Old_Node_Xml(filename)
            Add_Col_ToGview()
            Load_Xml_ToGview(filename)
        ElseIf mode = "New" Then
            Load_New_Node_Xml()
            Add_Col_ToGview()
        End If
    End Sub   
    Private Sub gVStory_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gVStory.CellClick
        Try
            Dim index, i As Integer
            index = gVStory.CurrentRow.Index
            For i = 0 To j - 1
                textbox(i).Text = gVStory.Item(i, index).Value
            Next
        Catch ex As Exception

        End Try

    End Sub
    Private Sub btnAdd_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Add_Value_ToGview()
        ClearText()
    End Sub
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Del_value_Gview()
        ClearText()
    End Sub
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Edit_value_Gview()
    End Sub
    Private Sub CreatXml(ByVal filename As String)
        Dim objWriter As New System.IO.StreamWriter(filename, False, System.Text.Encoding.UTF8)
        '  objWriter.WriteLine("<?xml version="1.0" & " encoding=" & "utf-8" & "?>")
        objWriter.WriteLine("<RootSubject>")
        objWriter.WriteLine("</RootSubject>")
        objWriter.Close()
    End Sub
    Private Sub Load_New_Node_Xml()
        Dim xmldoc As New XmlDocument
        Dim Doc As New XmlDocument
        Dim file_name As String
        arrNode.Clear()
        file_name = Application.StartupPath
        If file_name.EndsWith("\bin\Debug") Then
            file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
        End If
        file_name &= "\Xml\XmlConfigSubjects.xml"
        xmldoc.Load(file_name)
        Dim node As XmlNode = xmldoc.SelectSingleNode("/RootSubject/Subject[@type = 'Story']")
        Dim i As Integer = 10
        For Each n As XmlNode In node.ChildNodes
            Dim lble As Label = New Label
            arrNode.Add(n.Name)
            textbox(j) = New TextBox
            With lble
                .ForeColor = Color.Blue

                .Location = New Point(1, i)
                .Text = n.Name
                .TextAlign = ContentAlignment.BottomRight
                .Show()
            End With
            With textbox(j)
                .Name = "txt"
                .Location = New Point(110, i + 5)
                .Size = New Size(520, 25)
                .Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top

                .Show()
            End With
            Me.Controls.Add(lble)
            Me.Controls.Add(textbox(j))
            i = i + 27
            j = j + 1
        Next
        btnAdd.Location = New Point(91, i + 5)
        btnEdit.Location = New Point(216, i + 5)
        btnDel.Location = New Point(341, i + 5)
        btnsave.Location = New Point(514, i + 5)
        gVStory.Location = New Point(10, i + 10 + btnsave.Height)
        gVStory.Height = 250
        Me.Height = gVStory.Height + btnsave.Height + i + 60
        gVStory.Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom
    End Sub
    Private Sub Load_Old_Node_Xml(ByVal file_name As String)
        Try
            Dim xmldoc As New XmlDocument
            Dim Doc As New XmlDocument
            arrNode.Clear()
            xmldoc.Load(file_name)
            Dim node As XmlNode = xmldoc.SelectSingleNode("/RootSubject/Story[@id = '1']")
            Dim i As Integer = 10
            For Each n As XmlNode In node.ChildNodes
                Dim lble As Label = New Label
                arrNode.Add(n.Name)
                textbox(j) = New TextBox
                With lble
                    .ForeColor = Color.Blue

                    .Location = New Point(1, i)
                    .Text = n.Name
                    .TextAlign = ContentAlignment.BottomRight
                    .Show()
                End With
                With textbox(j)
                    .Name = "txt"
                    .Location = New Point(110, i + 5)
                    .Size = New Size(520, 25)
                    .Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top

                    .Show()
                End With
                Me.Controls.Add(lble)
                Me.Controls.Add(textbox(j))
                i = i + 27
                j = j + 1
            Next
            btnAdd.Location = New Point(91, i + 5)
            btnEdit.Location = New Point(216, i + 5)
            btnDel.Location = New Point(341, i + 5)
            btnsave.Location = New Point(514, i + 5)
            gVStory.Location = New Point(10, i + 10 + btnsave.Height)
            gVStory.Height = 250
            Me.Height = gVStory.Height + btnsave.Height + i + 60
            gVStory.Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom
        Catch ex As Exception
            MsgBox("The file isn't format", MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub Remove_Node_Xml(ByVal file_name As String)
        Dim xmldoc As New XmlDocument
        Dim Doc As New XmlDocument
        Dim SelNode As String = "/RootSubject"
        xmldoc.Load(file_name)
        Dim node As XmlNode = xmldoc.SelectSingleNode(SelNode)
        If node IsNot Nothing Then
            node.RemoveAll()
            xmldoc.Save(file_name)
        End If
    End Sub
    Private Sub Del_value_Gview()
        Try
            Dim index As Integer
            index = gVStory.CurrentRow.Index
            gVStory.Rows.RemoveAt(index)
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Edit_value_Gview()
        Try
            Dim index As Integer
            index = gVStory.CurrentRow.Index
            For i = 0 To j - 1
                gVStory.Rows(index).Cells(i).Value = textbox(i).Text
            Next
        Catch ex As Exception

        End Try

    End Sub
    Private Sub Add_Col_ToGview()
        For i = 0 To j - 1
            gVStory.Columns.Add(arrNode(i).ToString, arrNode(i).ToString)
        Next
    End Sub
    Private Sub Add_Value_ToGview()
        Dim val(j) As String
        For i = 0 To j - 1
            val(i) = textbox(i).Text
        Next
        gVStory.Rows.Add(val)
    End Sub
    Private Sub Load_Xml_ToGview(ByVal file_name As String)
        Try
            Dim val(j) As String
            'Dim valxml As New ArrayList
            Dim xmldoc As New XmlDocument
            Dim Doc As New XmlDocument
            xmldoc.Load(file_name)
            Dim node As XmlNode = xmldoc.SelectSingleNode("/RootSubject")
            For Each a As XmlNode In node.ChildNodes
                Dim i As Integer = 0
                For Each b As XmlNode In a.ChildNodes
                    val(i) = b.InnerText
                    i = i + 1
                Next
                gVStory.Rows.Add(val)
            Next
        Catch ex As Exception

        End Try

    End Sub
    Private Sub ClearText()
        For i = 0 To j - 1
            textbox(i).Text = String.Empty
        Next
    End Sub
    Private Sub DisableText()
        For i = 0 To j - 1
            textbox(i).Enabled = False
            textbox(i).BackColor = Color.White
        Next
    End Sub
    Private Sub EnableText()
        For i = 0 To j - 1
            textbox(i).Enabled = True
        Next
    End Sub
End Class


'Private Sub EditXml(ByVal file_name As String)
'    Dim xmldoc As New XmlDocument
'    Dim Doc As New XmlDocument
'    Try
'        Dim SelNode As String = "/RootSubject/Story[@id ='" & idXmlSelect & "']"
'        xmldoc.Load(file_name)
'        Dim node As XmlNode = xmldoc.SelectSingleNode(SelNode)
'        Dim i As Integer
'        For i = 0 To j - 1
'            node.ChildNodes(i).InnerText = textbox(i).Text
'        Next
'        xmldoc.Save(file_name)
'        MsgBox("Edit successful")
'    Catch ex As Exception
'        MsgBox("Delete Error", MsgBoxStyle.Critical)
'    End Try

'End Sub
'Private Sub DelXml(ByVal file_name As String)
'    Dim xmldoc As New XmlDocument
'    Dim Doc As New XmlDocument

'    Dim SelNode As String = "/RootSubject/Story[@id ='" & idXmlSelect & "']"
'    xmldoc.Load(file_name)
'    Dim node As XmlNode = xmldoc.SelectSingleNode(SelNode)
'    If node IsNot Nothing Then
'        node.ParentNode.RemoveChild(node)
'        xmldoc.Save(file_name)
'        MsgBox("Delete successful")

'    Else
'        MsgBox("Error Delete")
'    End If
'End Sub

'Chen truc tiep du lieu vao xml

'Private Sub InsertXml(ByVal file_name As String)
'    Dim Doc As New XmlDocument
'    Doc.Load(file_name)
'    ' Tạo một phần tử mới.
'    Dim ItemNode As XmlNode
'    ItemNode = Doc.CreateElement("Story")
'    ' Thêm đặc tính.
'    Dim Attribute As XmlAttribute
'    Attribute = Doc.CreateAttribute("id")
'    Attribute.Value = GetRecord(file_name) + 1
'    ItemNode.Attributes.Append(Attribute)
'    ' Tạo và thêm các phần tử con cho nút này.
'    Dim Node(Max) As XmlNode
'    Dim i As Integer
'    For i = 0 To j - 1
'        Node(i) = Doc.CreateElement(arrNode(i).ToString)
'        ItemNode.AppendChild(Node(i))
'        Node(i).AppendChild(Doc.CreateTextNode(Trim(textbox(i).Text)))
'        'MsgBox(textbox(i).Text)
'    Next
'    ' Thêm phần tử mới. Trong trường hợp này, chúng ta
'    ' cho nó làm con ở cuối danh sách item.
'    Doc.DocumentElement.AppendChild(ItemNode)
'    ' Lưu tài liệu.
'    Doc.Save(file_name)
'    MsgBox("Add successful")
'End Sub
'Private Sub LoadDataXml(ByVal file_name As String)
'    Dim ds As New DataSet()
'    Dim Doc As New XmlDocument
'    ds.ReadXml(file_name)
'    gVStory.Columns.Clear()
'    ' gVStory.DataMember = "Story"
'    Try
'        gVStory.DataSource = ds.Tables(0)
'    Catch ex As Exception
'        gVStory.DataSource = ""
'    End Try
'End Sub